package com.teamsun.service;

import com.teamsun.datasource.DynamicDataSource;
import com.teamsun.mapper.SequenceMapper;
import com.teamsun.mapper.SequenceMySQLMapper;
import com.teamsun.util.Constants;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashMap;
import java.util.Map;

/**
 * <p>获取数据库序列值</p>
 */
public class SequenceServiceImpl implements ISequenceService {

    @Autowired
    private SequenceMapper sequenceMapper;

    @Autowired
    private SequenceMySQLMapper sequenceMySQLMapper;

    @Autowired
    private DynamicDataSource dynamicDataSource;

    /**
     * 获取报表ID序列值
     * @return 报表ID
     */
    @Override
    public int getRptSequence() {
        // oracle根据Sequence获取报表ID，mysql数据库通过表保存报表ID
        String dbType = dynamicDataSource.getMasterDBType();
        if(Constants.DBTYPE_MYSQL.equalsIgnoreCase(dbType)) {
            int rptId = sequenceMySQLMapper.getRptId();
            int newId = rptId + 1;
            Map<String, Object> params = new HashMap<String, Object>();
            params.put("currValue", newId);
            sequenceMySQLMapper.updateRptId(params);
            return rptId;
        } else {
            return sequenceMapper.getRptId();
        }
    }
}
